import { create, remove, update, query,search,modify} from '../services/goodsInto'
import { parse } from 'qs'
import {Modal} from 'antd';

export default {
  namespace: 'goodsInto',

  state: {
    list: [],
    loading: false,
    currentItem: {},
    modalVisible: false,
    modalType: 'create',
    pagination: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null
    },
    item: {}
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen(location => {

        if (location.pathname === '/packages/goodsInto') {
          dispatch({
            type: 'search',
            payload: location.query
          })
        }
        //  if (location.pathname === '/packages/goodsInto') {
        //   if(location.query.keyname==undefined){
        //        dispatch({
        //         type: 'query',
        //         payload: location.query
        //       })
        //   }else{
        //      dispatch({
        //       type: 'search',
        //       payload: location.query
        //     })
        //   }         
        // }
        
      })
    }
  },

  effects: {
    *query ({ payload }, { call, put }) {
      // yield put({ type: 'showLoading' });

      let params=parse(payload)
      if(params.page==undefined){
        params.page=1
      }
      if(params.pageSize==undefined){
        params.pageSize=10
      }
      const data = yield call(query, parse(params));
      yield put({
        type: 'querySuccess',
        payload: {
          list: data.list,
          pagination: {
            total: data.totalRows,
            current: parseInt(params.page),
            pageSize: parseInt(params.pageSize),
          }
        }
      })
    },
    *'delete' ({ payload }, { call, put }) {
      yield put({ type: 'showLoading' });
      const data = yield call(remove, { id: payload });
      if (!data.errcode) {

        yield put({ type: 'query' });
      }else{
        yield put({ type: 'hideLoading' })
      }
    },
    *create ({ payload }, { call, put }) {
      yield put({ type: 'hideModal' });
      yield put({ type: 'showLoading' });
      const params = parse(payload);
      if(params.page==undefined){
        params.page=1
      }
      if(params.pageSize==undefined){
        params.pageSize=10
      }

      const data = yield call(create, params);

      if(data.errcode == 0){
        alert(data.errmsg || '上架失败');
      }else{
        alert("上架成功")
        document.getElementById('shopCodeNumber').value = '';
        // document.getElementById('shopCodeId').value = '';
        document.getElementById('shopCodeCount').value = '';
      }

      yield put({ type: 'reload' });
    },
    *search ({ payload }, { call, put }) {
        yield put({ type: 'showLoading' });
        let params=parse(payload)
        if(params.page==undefined){
          params.page=1
        }
        if(params.pageSize==undefined){
          params.pageSize=10
        }
      if(document.getElementById('goodsIntoCode')){
        params.code = document.getElementById('goodsIntoCode').value || '';
      }

      if(!document.getElementById('goodsIntoCode')){
        params.code = '';
      }
        const data = yield call(search, params);

        yield put({
          type: 'querySuccess',
          payload: {
            list: data.list,
            pagination:{
              total: data.totalRows,
              current: parseInt(params.page),
              pageSize: parseInt(params.pageSize),
            }
          }
        })
    },
    *update ({ payload }, { select, call, put }) {
      yield put({ type: 'hideModal' });
      yield put({ type: 'showLoading' });

      const data = yield call(update, payload);
        yield put({ type: 'reload' });
        const modal = Modal.success({
          title: '修改成功'
        });
        yield put({ type: 'hideLoading' })

    },
    *reload(action, { put, select }) {
      const page = yield select(({ goodsInto }) => goodsInto.pagination);
      yield put({ type: 'query', 
            payload: {
              page:page.current
           } 
        });
      yield put({ type: 'hideLoading' })
     },
  },

  reducers: {
    showLoading (state) {
      return { ...state, loading: true }
    },
    hideLoading (state) {
      return { ...state, loading: false }
    },
    querySuccess (state, action) {
      const {list, pagination} = action.payload;
      return { ...state,
        list,
        loading: false,
        pagination: {
          ...state.pagination,
          ...pagination
        }}
    },
    showModal (state, action) {
      console.log(action.payload)
      return { ...state, ...action.payload, modalVisible: true,item:action.payload.currentItem }
    },
    hideModal (state) {
      return { ...state, modalVisible: false }
    }
  }
}
